package com.um.springboot.starter.utils;

import com.um.springboot.starter.task.SpringAsyncRunner;
import com.um.springboot.starter.task.TaskExtendProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 甘天伟
 * @since 2024/6/28
 * 版权所有 广州优匠科技有限公司
 */
@Component
public class SpringAsyncUtil {

    @Autowired
    private TaskExtendProperties properties;

    @Autowired
    private SpringAsyncRunner springAsyncRunner;

    /**
     * 用默认线程池异步执行任务
     *
     * @param task 任务
     */
    public void run(Runnable task) {
        springAsyncRunner.run(task);
    }

    /**
     * 用次要线程池异步执行任务如果启用，否则用默认线程池执行
     *
     * @param task 任务
     */
    public void runBySecondary(Runnable task) {
        if (properties.getExecution().isSecondaryEnabled()) {
            springAsyncRunner.runBySecondary(task);
        } else {
            springAsyncRunner.run(task);
        }
    }
}
